package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ListView

/**
 * 自定义可以横向纵向滚动的ListView
 * @author ljk
 * @since  2019年4月15日
 */
class HVListView(context: Context?, attrs: AttributeSet?) : ListView(context, attrs) {
    /** 偏移坐标  */
    private var mOffset = 0
    /** 屏幕宽度  */
    private var screenWidth = 0
    /** 滚动View的宽度  */
    private var scrollWidth = 0
    /** 列头  */
    private var listHead: LinearLayout? = null
    /** 手势  */
    private val mGesture: GestureDetector
    var attrs: AttributeSet? = null
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        super.dispatchTouchEvent(ev)
        return mGesture.onTouchEvent(ev)
    }

    /**匿名内部类，处理监听到的手势事件 */
    private val gestureListener: GestureDetector.OnGestureListener = object : SimpleOnGestureListener() {
        override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean {
            synchronized(this@HVListView) {
                val moveX = distanceX.toInt() //沿着X轴移动的距离
                val curX = listHead!!.scrollX //当前X轴的滚动的位置
                if (screenWidth <= 0) screenWidth = width
                //System.out.println("滚动View的宽度:"+scrollWidth);
                var dx = moveX
                //控制越界问题
                if (curX + moveX < 0) dx = 0
                if (curX + moveX + getScreenWidth() > scrollWidth) {
                    dx = scrollWidth - getScreenWidth() - curX
                }
                mOffset += dx
                //根据手势滚动HVListView中每个Item视图
                var i = 0
                val j = childCount
                while (i < j) {
                    val child = (getChildAt(i) as ViewGroup).getChildAt(1)
                    if (child.scrollX != mOffset) {
                        child.scrollTo(mOffset, 0)
                    }
                    i++
                }
                //滚动HVListView的表头
                listHead!!.scrollBy(dx, 0)
            }
            //当view确定自身已经不再适合现有的区域时，该view本身调用这个方法要求parent view
//重新调用他的onMeasure onLayout来对重新设置自己位置
            requestLayout()
            return true
        }

        override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
            return false
        }

        override fun onDown(e: MotionEvent): Boolean {
            return true
        }
    }

    /**
     * 获取屏幕可见范围内最大屏幕
     * @return
     */
    fun getScreenWidth(): Int {
        if (screenWidth == 0) {
            screenWidth = context.resources.displayMetrics.widthPixels
            if (getChildAt(0) != null) { //减去固定列的宽度
                screenWidth -= (getChildAt(0) as ViewGroup).getChildAt(0).measuredWidth
            } else if (listHead != null) { //减去固定第一列
                screenWidth -= listHead!!.getChildAt(0).measuredWidth
            }
        }
        return screenWidth
    }

    /** 获取列头偏移量  */
    val headScrollX: Int
        get() = listHead!!.scrollX

    fun setListHead(listHead: LinearLayout?) {
        this.listHead = listHead
    }

    /**
     * 设置可横向滚动的空间宽度
     * 当最外层布局是RelativeLayout布局时必须设置，
     * 不然无法获取可滚动空间的宽度
     * @param scrollWidth 单位px
     */
    fun setScrollWidth(scrollWidth: Int) {
        this.scrollWidth = scrollWidth
    }

    init {
        mGesture = GestureDetector(context, gestureListener)
        /*this.attrs = attrs;
		int l = attrs.getAttributeIntValue(android.R.attr.layout_width, 0);
		System.out.println("layout_width:"+l);*/
    }
}
